package course.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import course.dao.IUserDao;
import course.db.DbConnection;
import course.po.User;

public class UserDaoImpl implements IUserDao {
	/**
	 * 登录验证
	 * 
	 * @author yangliping
	 *
	 */
	@Override
	public User login(DbConnection con, User user) {
		Connection conn = DbConnection.getConnection();
		User resultUser = null;
		String sql = "SELECT * FROM user WHERE userName=? AND password=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getPassWord());
			rs = ps.executeQuery();
			while (rs.next()) {
				int id = rs.getInt(1);
				String userName = rs.getString(2);
				String password = rs.getString(3);
				resultUser = new User();
				resultUser.setId(id);
				resultUser.setUserName(userName);
				resultUser.setPassWord(password);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(rs, ps);
		}
		return resultUser;

	}

	/**
	 * 判断是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否不是空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		if (str != null && "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}
}
